package cn.lanqiao.system.service.impl;

import cn.lanqiao.common.dto.OrderDTO;
import cn.lanqiao.common.dto.OrderDetailTDO;
import cn.lanqiao.common.entity.Film;
import cn.lanqiao.common.entity.Order;
import cn.lanqiao.common.entity.OrderDetail;
import cn.lanqiao.common.vo.OrderFilmVo;
import cn.lanqiao.system.mapper.FilmMapper;
import cn.lanqiao.system.mapper.OrderDetailMapper;
import cn.lanqiao.system.mapper.OrderMapper;
import cn.lanqiao.system.service.OrderService;
import cn.lanqiao.system.vo.OrderDetailVo;
import cn.lanqiao.system.vo.OrderVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Repository
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper ;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private FilmMapper filmMapper;

    @Override
    public PageInfo<OrderVo> getPage(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<OrderVo> orderVoList = orderMapper.getAllOrder();
        for(OrderVo vo : orderVoList){
            List<OrderDetailVo> od = orderMapper.getOrderDetailByOrderId(vo.getOrderId());
            vo.setOrderDetailVOList(od);
        }
        PageInfo<OrderVo> page = new PageInfo<>(orderVoList);
        return page;
    }

    @Override
    public PageInfo<OrderVo> getPageInformation(int pageNo, int pageSize, String orderCode, String userPhone) {
        PageHelper.startPage(pageNo,pageSize);
        List<OrderVo> orderVoList = orderMapper.getInformation(orderCode, userPhone);
        for(OrderVo vo : orderVoList){
            List<OrderDetailVo> od = orderMapper.getOrderDetailByOrderId(vo.getOrderId());
            vo.setOrderDetailVOList(od);
        }
        PageInfo<OrderVo> page = new PageInfo<>(orderVoList);
        return page;
    }

    @Override
    public int updateOrderStatus(Integer id, Integer status) {
        return orderMapper.updateOrderStatus(id, status);
    }

    @Override
    public List<OrderDetailVo> getOrderByUserId(Integer userId) {
        return orderMapper.getOrderByUserId(userId);
    }

    @Override
    public int createOrder(OrderDTO orderDTO) {
        //订单表新增一条记录
        Order order=new Order();
        //生成订单编号
        String maxCode=orderMapper.getMaxOrderCode();
        maxCode=maxCode==null ? "20221006" : maxCode;
        String newOrderCode=Integer.parseInt(maxCode)+1+"";
        //保存
        order.setOrderCode(newOrderCode);
        //设置订单用户Id
        order.setUserId(orderDTO.getUserId());
        //设置场次id
        order.setAId(orderDTO.getAid());
        //设置座位
        order.setSeat(orderDTO.getSeat());
        //获取座位数量
        String s=orderDTO.getSeat();
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<s.length();i++){
            String ss=String.valueOf(s.charAt(i));
            if(ss.equals("号")){
                list.add(i);
            }
        }

        int a=list.size();
        orderDTO.setCount(a);

        //设置订单总价
        order.setPrice(orderDTO.getPrice());
        //设置订单状态
        order.setStatus(orderDTO.getStatus());
        //写入订单Id
        Integer maxOrderId=orderMapper.getMaxOrderId();
        maxOrderId=maxOrderId==null ? 1 : maxOrderId;
        Integer newOrderId=maxOrderId+1;
        order.setId(newOrderId);
        //设置下单时间
        order.setTime(new Date());
        //设置完成时间
        order.setEndTime(new Date());
        //添加到订单
        orderMapper.insertSelective(order);

            //添加订单详情表内容
            OrderDetail orderDetail=new OrderDetail();
            orderDetail.setFilmId(orderDTO.getFilmId());
            orderDetail.setCount(orderDTO.getCount());
            orderDetail.setOrderId(order.getId());
            //添加订单详情表
            orderDetailMapper.insertSelective(orderDetail);
            //用户收藏列表的数据删除
           // cartMapper.deleteByPrimaryKey(dto.getCartId);
            //电影表库存修改
            //获取电影院库存
            Film film=filmMapper.selectByPrimaryKey(orderDTO.getFilmId());
            Integer oldCount=film.getCount();
        System.out.println("这是旧的座位数"+oldCount);
        System.out.println("这是购买的座位数"+orderDTO.getCount());
            //封装发送给sql语句的数据
            Film f=new Film();
            f.setId(orderDTO.getFilmId());
            f.setCount(oldCount-orderDTO.getCount());

        System.out.println("这是刷新的座位数："+f.getCount());
            filmMapper.updateByPrimaryKeySelective(f);


        return 0;
    }
    //按场次id查询订单

    @Override
    public List<Order> findByOrderId(Integer id) {
        return orderMapper.findByOrderId(id);
    }
    //按用户id查看订单详情(用户模块)
    @Override
    public List<OrderFilmVo> find(Integer id) {
        return orderMapper.find(id);
    }
}
